local CommonOHOH = _G.CommonOHOH;
local M = CommonOHOH.class.new(script, CommonOHOH.files.ModelObject());

local UserNameList={}
local UserHeadList={}

function FindHead(userId)

	if UserHeadList[userId] then
		return UserHeadList[userId]
	else
		local head=game.Players:GetUserThumbnailAsync(userId, Enum.ThumbnailType.HeadShot,Enum.ThumbnailSize.Size420x420)
		if head then
			UserHeadList[userId]=head
			return UserHeadList[userId]
		else

			return ""
		end
	end
     
end

function FindName(userId)
	if UserNameList[userId] then
		return UserNameList[userId]
	else
		local name=game.Players:GetNameFromUserIdAsync(userId)
		if name then
			UserNameList[userId]=name
			return UserNameList[userId]
		else

			return "userId:"..userId
		end
	end
end


function M.new(...)
	return M(...);
end;

function M.ctor(self,model,fullFuc)--构造函数

	assert(model,"model==nul")

	M.super.ctor(self, model);
	

end;

function M.InitRank(self,fullFuc)
	self.RankList={}
	
	self.cell=self:WaitForChild_Loop("rank.SurfaceGui.Main.ScrollingFrame.cell")
	self.cell.Visible=false

	local c1=self:WaitForChild_Loop("rank.SurfaceGui.Main.ScrollingFrame.1")
	local c2=self:WaitForChild_Loop("rank.SurfaceGui.Main.ScrollingFrame.2")
	local c3=self:WaitForChild_Loop("rank.SurfaceGui.Main.ScrollingFrame.3")

	table.insert(self.RankList,c1)
	table.insert(self.RankList,c2)
	table.insert(self.RankList,c3)

	for _,v  in pairs(self.RankList) do
		v.Visible=false
	end
	
	self.th1=self:WaitForChild_Loop("rank.SurfaceGui.Main.head.1th.Icon")
	self.th2=self:WaitForChild_Loop("rank.SurfaceGui.Main.head.2th.Icon")
	self.th3=self:WaitForChild_Loop("rank.SurfaceGui.Main.head.3rd.Icon")

	self.th1.Image=""
	self.th2.Image=""
	self.th3.Image=""

	self.fullFuc=fullFuc
end



function M.SetTitle(self,text)
  
	local lable=  self:WaitForChild_Loop("title.SurfaceGui.Frame.TextLabel")
    lable.Text=text
end


function M.Update(self,data)-- 更新数据

	self:AddProcess(function()
		for k,v in pairs(data) do
			local cell=nil
			if self.RankList[k] then
				cell=self.RankList[k]
			else
				cell=self.cell:Clone()
				cell.Parent=self.cell.Parent
				self.RankList[k]=cell
				-- table.insert(self.RankList,cell)
			end
			cell.Visible=true
	
			local  nickName =FindName(v.key)
			
			cell.name.Text=nickName
	
			if k==1 then
				self.th1.Image=FindHead(v.key)
			elseif k==2 then
				self.th2.Image=FindHead(v.key)
			elseif k==3 then
				self.th3.Image=FindHead(v.key)
			end
	
			cell.rank.Text=k
	
			if self.fullFuc then
				self.fullFuc(cell.value,v.value,v.key)
			else
				cell.value.Text=v.value
			end
			self:Wait(0.2)
		end
	
	end)

end;




function M.dtor(self)--析构函数
	M.super.dtor(self);
end;

return M